package View;

/**
 * Creates a floor view for the user to choose up or down
 * When the user chooses a hall call the request is sent to the dispatcher
 * 
 * @author Varun Maryada, Sanat Dixit, Steve Tannahill
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import Dispatcher.GroupDispatcher;

@SuppressWarnings("serial")
public class FloorGUI extends JPanel implements ActionListener {

	private int floor;
	private JButton up;
	private JButton down;

	private GroupDispatcher gd;

	/**
	 * Constructs Floor Panel
	 * 
	 * @param floor
	 * @param gd
	 */
	public FloorGUI(int floor, GroupDispatcher gd) {
		this.gd = gd;
		this.floor = floor;
		this.setLayout(new GridLayout(1, 2));
		this.createButtons();
	}

	/**
	 * Creates the up and down buttons and adds it to the panel
	 */
	public void createButtons() {
		if (floor == 1) {
			up = new JButton("UP");
			up.addActionListener(this);
			this.add(up, BorderLayout.CENTER);
		} else if (floor == 15) {
			down = new JButton("DOWN");
			down.addActionListener(this);
			add(down, BorderLayout.CENTER);
		} else {
			up = new JButton("UP");
			up.addActionListener(this);
			this.add(up, BorderLayout.WEST);
			down = new JButton("DOWN");
			down.addActionListener(this);
			add(down, BorderLayout.EAST);
		}

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == up) {
			this.gd.processHallCall(this.floor, 1);
		} else if (e.getSource() == down) {
			this.gd.processHallCall(this.floor, -1);
		}
	}

}
